var ScenePlotLibrary = function(options) {
	this.ctx = options.ctx;
	this.editor = options.editor;
	this.globe = options.editor.globe;
	this.sceneId = options.sceneId;
	this.isOpen = false;
}

ScenePlotLibrary.prototype.open = function() {
	var _this = this;
	if (_this.isOpen) {
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/plot/library", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.closeAll();
	layer.open({
		id: 'scenePlotLibrary',
		title: '点样式库',
		type: 1,
		area: '220px',
		content: res,
		shade: 0,
		offset: 'l',
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpen = true;
			_this.dIndex = dIndex;
			$(layero).children('.layui-layer-content').css('overflow', 'hidden');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
		},
		end: function() {
			_this.isOpen = false;
			_this.dIndex = null;
			if(_this.drawModel){
				_this.drawModel.destroy();
				_this.drawModel=null;
			}
		}
	});
}


ScenePlotLibrary.prototype.loadList = function(classifyId) {
	var _this = this;
	var search = form.val('selectModelSearchForm');
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/listDataForModel", { classifyId: classifyId || $("#classify-select").val() }, "json", null);
	if (0 == res.code) {
		res.ctx = _this.ctx;
		laytpl(selectModelsScript.innerHTML).render(res, function(html) {
			$('#select-model-list').html(html);
			$(".ImageList>.content>.item>.Thumbnail").on('click', function() {
				_this.add($(this).attr('id'));
			})
		});
	}
}

ScenePlotLibrary.prototype.add=function(id){
	let _this=this;
	if(_this.drawModel){
		_this.drawModel.destroy();
		_this.drawModel=null;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/getBySrcId", {
		thingId:id,
	}, "json", null);
	layer.closeAll('loading');
    if (200==res.code) {
    	let data=res.data;
    	let options={
    		id:	_this.globe.getUuid(),
    		name:data.name,
    		code:data.code||"",
    		type:'2',
    		thing:{
				show:true,
				url:(data.isRelative=='0') ?data.url:_this.ctx+data.url,
			}
    	}
    	_this.drawModel=_this.globe.drawPModel(options);
    	_this.drawModel.on('success',function(position){
    		if(position){
                var formData={}; 
                formData['sceneModel.id']=options.id;
                formData['sceneModel.name']=options.name;
                formData['sceneModel.code']=options.code;
                formData['sceneModel.type']=options.type;
                formData['sceneModel.x']=position.x;
                formData['sceneModel.y']=position.y;
                formData['sceneModel.z']=position.z;
                formData['sceneModel.thingId']=data.id;
                formData['sceneModel.sceneId']=_this.sceneId;
                layer.load(2);
    			var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/save",formData, "json", null);
    			layer.closeAll('loading');
    			if(200==res.code){
    				layer.msg(res?res.message:'系统异常!', {icon: 1});
        			options.position=position;
        			_this.globe.render(options);
    			}else{
    				 layer.msg(res?res.message:'系统异常!', {icon: 2});
    			}
    		}
    	});
    	_this.drawModel.on('error',function(e){
    	});
    	_this.drawModel.on('complete',function(e){
    	});
    } else {
        layer.msg(res?res.message:'系统异常!', {icon: 2});
    }
}

ScenePlotLibrary.prototype.editPModel=function(obj){
	var _this = this;
	_this.model=obj;
	if (_this.isOpenEditPModel) {
		_this.closeEditTool();
		_this.loadForm(obj);
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/model/edit", {}, "html", null);
	layer.closeAll('loading');
	if (!res) {
		layer.alert('系统异常！');
		return;
	}
	
	layer.open({
		id: 'editPModel',
		title: '属性编辑',
		type: 1,
		offset: '40px',
		area: '300px',
		content: res,
		shade: 0,
		offset: 'l',
		skin: 'layui-layer layui-layer-adminRight layui-layer-city',
		success: function(layero, dIndex) {
			_this.isOpenEditPModel = true;
			_this.editPModelIndex = dIndex;
			$(layero).children('.layui-layer-content').css('overflow', 'hidden');
			$(layero).children('.layui-layer-content').css('height', 'calc(100% - 30px)');
			_this.loadForm(obj);
			form.render();
			$("#pmodelTranslationBtn").on('click', function() {
				if(!$(this).hasClass("scene-btn-disabled")){
					$(this).addClass("scene-btn-disabled");
					_this.closeEditTool();
				}else{
					$(this).removeClass("scene-btn-disabled");
					_this.createEditTool(_this.model);
				}
			})
		},
		end: function() {
			_this.isOpenEditPModel = false;
			_this.editPModelIndex = null;
			_this.closeEditTool();
		}
	});
}

ScenePlotLibrary.prototype.loadForm = function(model){
	var _this = this;
	var data=this.getFormData(model);
	form.val('modelEditForm', data);
	
	_this.showTr(data);
	_this.createEditTool(model);
	
	$('#sceneModelName').on('input', function() {
		 if(model){
			 model.updateAttribute({
				 name:$("#sceneModelName").val(),
			 });
		 }
	 });
	$('#sceneModelCode').on('input', function() {
		 if(model){
			 model.updateAttribute({
				 code:$("#sceneModelCode").val()
			 });
		 }
	 });
	$('#sceneModelSort').on('input', function() {
		 if(model){
			 model.updateAttribute({
				 sort:$("#sceneModelSort").val()
			 });
		 }
	 });
	
	$('#sceneModelScale').on('input', function() {
		 if(model){
			 model.updateScale($("#sceneModelScale").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });
	
	$('#sceneModelHeading').on('input', function() {
		 if(model){
			 model.updateHeading($("#sceneModelHeading").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });
	
	$('#sceneModelPitch').on('input', function() {
		 if(model){
			 model.updatePitch($("#sceneModelPitch").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });	 
	 
	$('#sceneModelRoll').on('input', function() {
		 if(model){
			 model.updateRoll($("#sceneModelRoll").val());
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });	
	
	$('#sceneModelMinimumPixelSize').on('input', function() {
		 if(model){
			 model.updateMinimumPixelSize($("#sceneModelMinimumPixelSize").val());
		 }
	});
	
	$('#modelZ').on('input', function() {
		 if(model){
			 model.updatePosition({
				 x:$("#modelX").val(),
				 y:$("#modelY").val(),
				 z:$("#modelZ").val(),
			 });
			 if(_this.editPModelTool){
				 _this.editPModelTool.update(model);
			 }
		 }
	 });
	
	colorpicker.render({//间隔颜色
		elem: '#fillColor',
		color: (data&&data.fillColor)?data.fillColor:null,
		format: 'rgb',
		predefine: true,
		alpha: true,
		done: function(color) {
			$('#fillColor-input').val(color); //向隐藏域赋值
			color || this.change(color); //清空时执行 change
			 if(model){
				 model.updateColor(color);
			 }
		},
		change: function(color) {
		}
	});
	
	colorpicker.render({//间隔颜色
		elem: '#silhouetteColor',
		color: (data&&data.silhouetteColor)?data.silhouetteColor:null,
		format: 'rgb',
		predefine: true,
		alpha: true,
		done: function(color) {
			$('#silhouetteColor-input').val(color); //向隐藏域赋值
			color || this.change(color); //清空时执行 change
			 if(model){
				 model.updateSilhouetteColor(color);
			 }
		},
		change: function(color) {}
	});
	
	$('#sceneModelSilhouetteSize').on('input', function() {
		 if(model){
			 model.updateSilhouetteSize($("#sceneModelSilhouetteSize").val());
		 }
	 });
	
	 $('#sceneModelDistanceNear').on('input', function() {
		 if(model){
			 model.updateDistanceDisplay({
				 distanceNear:$("#sceneModelDistanceNear").val(),
				 distanceFar:$("#sceneModelDistanceFar").val(),
			 });
		 }
	 });
	 $('#sceneModelDistanceFar').on('input', function() {
		 if(model){
			 model.updateDistanceDisplay({
				 distanceNear:$("#sceneModelDistanceNear").val(),
				 distanceFar:$("#sceneModelDistanceFar").val(),
			 });
		 }
	 });	
	
	$('#sceneModelPositionBtn').click(function() {
		_this.globe.flyToPoisition(data);
	});
	
	$('#sceneModelDeleteBtn').click(function() {
		layer.confirm('确定要删除模型吗？', {
			shade: .1
		}, function(index) {
			_this.delete(data);
		}, function(index) {
			layer.close(index);
			return false;
		});
	});
	
	form.on('select(fill)', function(data){
		if(data.value==1){
			$('#sceneModel-fillColor-tr').show();
		}else {
			$('#sceneModel-fillColor-tr').hide();
		}
		 if(model){
			 model.isFill(data.value);
		 }
	});
	
	form.on('select(silhouette)', function(data){
		if(data.value==1){
			$('#sceneModel-silhouetteColor-tr').show();
			$('#sceneModel-silhouetteSize-tr').show();
		}else {
			$('#sceneModel-silhouetteColor-tr').hide();
			$('#sceneModel-silhouetteSize-tr').hide();
		}
		 if(model){
			 model.isSilhouette(data.value);
		 }
	});
	
	form.on('select(distanceDisplayCondition)', function(data) {
		 if('0'==data.value){
			 $("#sceneModel-distanceNear-tr").css("display",'none');
			 $("#sceneModel-distanceFar-tr").css("display",'none');
		 }else{
			 $("#sceneModel-distanceNear-tr").css("display",'');
			 $("#sceneModel-distanceFar-tr").css("display",'');
		 }
		 if(model){
			 model.isDistanceDisplay(data.value);
		 }
	 });

	 form.on('radio(shadows)', function(data){
		 if(model){
			 model.isShadows(data.value);
		 };
     });

	 form.on('radio(animation)', function(data){
		 if(model){
			 model.isAnimation(data.value);
		 };
     });
	 
	form.on('submit(submitModelEditForm)', function(data) {
		layer.load(2);
		var formData = {};
		for (var key in data.field) {
			formData['sceneModel.' + key] = data.field[key];
		}
		var res = common_ajax.ajaxFunc('post', _this.ctx + "/scene/model/save", formData, "json", null);
		layer.closeAll('loading');
		if (res && 200 == res.code) {
			layer.msg(res.message, {icon: 1});
			_this.editor.sceneLayer.refreshNode();
		} else {
			layer.msg(res.message, {icon: 2});
		}
		return false;
	});
}

ScenePlotLibrary.prototype.showTr=function(data){
	if(data.fill==1){
		$('#sceneModel-fillColor-tr').show();
	}
	if(data.silhouette==1){
		$('#sceneModel-silhouetteColor-tr').show();
		$('#sceneModel-silhouetteSize-tr').show();
	}
	if(data.distanceDisplayCondition==1){
		$("#sceneModel-distanceNear-tr").show();
		$("#sceneModel-distanceFar-tr").show();
	}
	
}

ScenePlotLibrary.prototype.getFormData=function(obj){
	let data={};
	if(obj){
		data={
			id:obj.id,
			name:obj.name||'',
			code:obj.code||'',
			
			x:obj.position.x||'',
			y:obj.position.y||'',
			z:obj.position.z||0,
			
			heading:obj.orientation.heading||0,
			pitch:obj.orientation.pitch||0,
			roll:obj.orientation.roll||0,
			
			scale:obj.model.scale||1,
			minimumPixelSize:obj.model.minimumPixelSize||0,
			fill:obj.model.fill||0,
			fillColor:obj.model.fillColor||'',
			silhouette:obj.model.silhouette||0,
			silhouetteColor:obj.model.silhouetteColor||'',
			sceneModelSilhouetteSize:obj.model.sceneModelSilhouetteSize||0,
			distanceDisplayCondition:obj.model.distanceDisplayCondition||0,
			distanceNear:obj.model.distanceNear||0,
			distanceFar:obj.model.distanceFar||5000,
			animation:obj.model.animation||0,
			shadows:obj.model.shadows||false,
		}	
	}
	return data;
}

ScenePlotLibrary.prototype.delete = function(obj){
	let _this=this;
	if(!obj){
		layer.alert('删除失败，刷新重试！');
		return;
	}
	layer.load(2);
	var res = common_ajax.ajaxFunc('get', _this.ctx + "/scene/layer/delete", {layerId:obj.id},"json", null);
	layer.closeAll('loading');
	if (res && 200==res.code) {
        layer.msg(res.message, {icon: 1});
		layer.close(_this.pdIndex);
		_this.globe.remove(obj.id);
        return true;
    } else {
        layer.msg(res.message, {icon: 2});
        return false;
    }
}

ScenePlotLibrary.prototype.createEditTool=function(model){
	let _this=this;
	_this.editPModelTool=_this.globe.createEditPModel(model);
	_this.editPModelTool.on('change',function(e){
		if(e.scale){
			$("#sceneModelScale").val(e.scale)
		}
		if(e.heading){
			$("#sceneModelHeading").val(e.heading)
		}
		if(e.position){
			$("#modelX").val(e.position.x)
			$("#modelY").val(e.position.y)
			$("#modelZ").val(e.position.z)
		}
	});
}

ScenePlotLibrary.prototype.closeEditTool=function(){
	let _this=this;
	if(_this.editPModelTool){
		_this.editPModelTool.destroy();
		_this.editPModelTool=null;
	}
}

ScenePlotLibrary.prototype.close = function(){
	var _this = this;
	if(_this.editPModelIndex){
		layer.close(_this.editPModelIndex);	
	}
}

